/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import com.applicatioN.entities.Exiger1;
import com.applicatioN.entities.Exiger1PK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Poste;
import com.applicatioN.entities.Langue;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class Exiger1JpaController implements Serializable {

    public Exiger1JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Exiger1 exiger1) throws PreexistingEntityException, Exception {
        if (exiger1.getExiger1PK() == null) {
            exiger1.setExiger1PK(new Exiger1PK());
        }
        exiger1.getExiger1PK().setIntitule(exiger1.getLangue().getIntitule());
        exiger1.getExiger1PK().setNomPoste(exiger1.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Poste poste = exiger1.getPoste();
            if (poste != null) {
                poste = em.getReference(poste.getClass(), poste.getNomPoste());
                exiger1.setPoste(poste);
            }
            Langue langue = exiger1.getLangue();
            if (langue != null) {
                langue = em.getReference(langue.getClass(), langue.getIntitule());
                exiger1.setLangue(langue);
            }
            em.persist(exiger1);
            if (poste != null) {
                poste.getExiger1Collection().add(exiger1);
                poste = em.merge(poste);
            }
            if (langue != null) {
                langue.getExiger1Collection().add(exiger1);
                langue = em.merge(langue);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExiger1(exiger1.getExiger1PK()) != null) {
                throw new PreexistingEntityException("Exiger1 " + exiger1 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Exiger1 exiger1) throws NonexistentEntityException, Exception {
        exiger1.getExiger1PK().setIntitule(exiger1.getLangue().getIntitule());
        exiger1.getExiger1PK().setNomPoste(exiger1.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger1 persistentExiger1 = em.find(Exiger1.class, exiger1.getExiger1PK());
            Poste posteOld = persistentExiger1.getPoste();
            Poste posteNew = exiger1.getPoste();
            Langue langueOld = persistentExiger1.getLangue();
            Langue langueNew = exiger1.getLangue();
            if (posteNew != null) {
                posteNew = em.getReference(posteNew.getClass(), posteNew.getNomPoste());
                exiger1.setPoste(posteNew);
            }
            if (langueNew != null) {
                langueNew = em.getReference(langueNew.getClass(), langueNew.getIntitule());
                exiger1.setLangue(langueNew);
            }
            exiger1 = em.merge(exiger1);
            if (posteOld != null && !posteOld.equals(posteNew)) {
                posteOld.getExiger1Collection().remove(exiger1);
                posteOld = em.merge(posteOld);
            }
            if (posteNew != null && !posteNew.equals(posteOld)) {
                posteNew.getExiger1Collection().add(exiger1);
                posteNew = em.merge(posteNew);
            }
            if (langueOld != null && !langueOld.equals(langueNew)) {
                langueOld.getExiger1Collection().remove(exiger1);
                langueOld = em.merge(langueOld);
            }
            if (langueNew != null && !langueNew.equals(langueOld)) {
                langueNew.getExiger1Collection().add(exiger1);
                langueNew = em.merge(langueNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Exiger1PK id = exiger1.getExiger1PK();
                if (findExiger1(id) == null) {
                    throw new NonexistentEntityException("The exiger1 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Exiger1PK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger1 exiger1;
            try {
                exiger1 = em.getReference(Exiger1.class, id);
                exiger1.getExiger1PK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exiger1 with id " + id + " no longer exists.", enfe);
            }
            Poste poste = exiger1.getPoste();
            if (poste != null) {
                poste.getExiger1Collection().remove(exiger1);
                poste = em.merge(poste);
            }
            Langue langue = exiger1.getLangue();
            if (langue != null) {
                langue.getExiger1Collection().remove(exiger1);
                langue = em.merge(langue);
            }
            em.remove(exiger1);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Exiger1> findExiger1Entities() {
        return findExiger1Entities(true, -1, -1);
    }

    public List<Exiger1> findExiger1Entities(int maxResults, int firstResult) {
        return findExiger1Entities(false, maxResults, firstResult);
    }

    private List<Exiger1> findExiger1Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Exiger1.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Exiger1 findExiger1(Exiger1PK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Exiger1.class, id);
        } finally {
            em.close();
        }
    }

    public int getExiger1Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Exiger1> rt = cq.from(Exiger1.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
